package cn.ffcs.uec.wBStaffInfo.action;

import java.text.ParseException;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.common.util.NodeVoUtil;
import cn.ffcs.uec.common.util.UecConstants;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.model.UecFamily;
import cn.ffcs.uec.wBStaffInfo.action.bean.WbUecOutStaffFamilyEditBean;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;

@Controller
@Scope("prototype")
public class WbUecOutStaffFamilyEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private WbUecOutStaffFamilyEditBean bean = new WbUecOutStaffFamilyEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecOutStaffInfo uecStaffInfo;
	/**
	 * 人员家庭信息
	 */
	private UecFamily uecFamily;

	private boolean isOutStaffEntryExt=false;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 * 
	 * @throws ParseException
	 */
	public void onCreate$wbUecFamilyEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {
		// 性别
		ListboxUtils.rendererForEdit(bean.getGlbdef1(),
				NodeVoUtil.getDictionaries(UecConstants.SEX));
		// 政治面貌
		ListboxUtils.rendererForEdit(bean.getPolitics(),
				NodeVoUtil.getDictionaries(UecConstants.POLITICAL_OUTLOOK));
		// 家庭成员状态
		ListboxUtils.rendererForEdit(bean.getGlbdef2(),
				NodeVoUtil.getDictionaries(UecConstants.FAMILY_MEMBER_STATE));
	}

	/**
	 * bindBean
	 * 
	 * @throws ParseException
	 */
	public void bindBean() throws ParseException {
		opType = (String) arg.get("opType");
		
		if ("add".equals(opType)) {
			this.bean.getWbUecFamilyEditWindow().setTitle("新增人员家庭信息");
			bean.getUecFamilyEditDiv().setHeight("30%");
			uecStaffInfo = (UecOutStaffInfo) arg.get("uecOutStaffInfo");
		}
		if ("view".equals(opType)) {
			this.bean.getWbUecFamilyEditWindow().setTitle("查看人员家庭信息");
			uecFamily = (UecFamily) arg.get("uecFamily");
			if (null != uecFamily) {
				PubUtil.fillBeanFromPo(uecFamily, bean);
				stringTransToDate();
				bean.getButtonDiv().setVisible(false);
				PubUtil.disableComponent(bean.getWbUecFamilyEditWindow(), true);
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getWbUecFamilyEditWindow().setTitle("编辑人员家庭信息");
			bean.getUecFamilyEditDiv().setHeight("30%");
			uecFamily = (UecFamily) arg.get("uecFamily");
			if (null != uecFamily) {
				PubUtil.fillBeanFromPo(uecFamily, bean);
				stringTransToDate();
			}
		}
		if ("approvalEdit".equals(opType)) {
			this.bean.getWbUecFamilyEditWindow().setTitle("审批人员家庭信息修改");
			bean.getUecFamilyEditDiv().setHeight("30%");
			uecFamily = (UecFamily) arg.get("uecFamily");
			if (null != uecFamily) {
				PubUtil.fillBeanFromPo(uecFamily, bean);
				stringTransToDate();
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		// 获取登录用户
		String currentName = "";
		if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
			ZkUtil.showError("当前登录用户已经失效，请重新登录系统", "错误提示");
			return;
		} else {
			currentName = PlatformUtil.getCurrentUser().getScreenName();
		}
		if ("add".equals(opType)) {
			instantOfUecFamily(currentName);
			if (!isOutStaffEntryExt) {
				uecFamily.addOnly();
			}
			Events.postEvent("onOK", this.self, uecFamily);
			this.onCancel();
			Messagebox.show("新增成功！");
		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecFamily);
			dateTransToString();
			uecFamily.setModifier(currentName);
			uecFamily.setModifiedtime(DateUtil.getCurrenDate());
			if (!isOutStaffEntryExt) {
				uecFamily.updateOnly();
			}
			Events.postEvent("onOK", this.self, uecFamily);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
		if ("approvalEdit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecFamily);
			dateTransToString();
			uecFamily.setModifier(currentName);
			uecFamily.setModifiedtime(DateUtil.getCurrenDate());
			uecFamily.updateOnly();
			Events.postEvent("onOK", this.self, uecFamily);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
	}

	/**
	 * 新增一个uecFamily对象
	 * 
	 * @param currentName
	 */
	private void instantOfUecFamily(String currentName) {
		uecFamily = UecFamily.newInstance();
		PubUtil.fillPoFromBean(bean, uecFamily);
		dateTransToString();
		uecFamily.setStaffId(uecStaffInfo.getOutStaffId());
		uecFamily.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
		uecFamily.setPkPsndoc(uecStaffInfo.getPkPsnMsg());
		uecFamily.setCreator(currentName);
		uecFamily.setCreationtime(DateUtil.getCurrenDate());
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getWbUecFamilyEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		if (StrUtil.isNullOrEmpty(bean.getMemName().getValue())) {
			return "请填写家庭成员姓名";
		} else if (StrUtil.isNullOrEmpty(bean.getMemRelation().getValue())) {
			return "请填写与本人关系";
		} else if (StrUtil.isNullOrEmpty(bean.getGlbdef1().getSelectedItem()
				.getValue())) {
			return "请选择该成员的性别";
		} else if (StrUtil.isNullOrEmpty(bean.getGlbdef2().getSelectedItem()
				.getValue())) {
			return "请选择该成员状态";
		} else if (StrUtil.isNullOrEmpty(bean.getPolitics().getSelectedItem()
				.getValue())) {
			return "请选择该成员的政治面貌";
		} else if (StrUtil.isNullOrEmpty(bean.getMemCorp().getValue())) {
			return "请填写工作单位";
		} else if (StrUtil.isNullOrEmpty(bean.getRelaaddr().getValue())) {
			return "请填写联系地址";
		} else if (StrUtil.isNullOrEmpty(bean.getRelaphone().getValue())) {
			return "请填写联系电话";
		}
		return null;
	}

	/**
	 * 将Date类型时间转成String类型 Date --> String 一套模型，两套标准：日期类型需要格式化
	 */
	public void dateTransToString() {
		if (!StrUtil.isNullOrEmpty(bean.getMemBirthday().getValue())) {
			// 出生日期
			uecFamily.setMemBirthday(DateUtil.dateToStr(bean.getMemBirthday()
					.getValue()));
		}
	}

	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		// 出生日期
		if (null != uecFamily.getMemBirthday()) {
			bean.getMemBirthday().setValue(
					DateUtil.patternStrToDate(uecFamily.getMemBirthday()));
		}
	}

}
